//
//  zhaocanpinViewController.m
//  hainingOASystem
//
//  Created by michael on 2017/11/15.
//  Copyright © 2017年 czd. All rights reserved.
//
#import "cpxqTableViewController.h"
#import "canpinTableViewCell.h"
#import "zhaocanpinViewController.h"
#import "UIView+Extension.h"
@interface zhaocanpinViewController ()

@end

@implementation zhaocanpinViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"找产品";
    _string = kchaxun;
    _currentPage = 1;
    self.view.backgroundColor = [UIColor whiteColor];
    UIView *view = [[UIView alloc]initWithFrame:self.view.frame];

UIColor *myColor = [UIColor colorWithWhite:0.5 alpha:0.1];
view.backgroundColor = myColor;
[self.view addSubview:view];
    [self requsetdata];
    _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
 __weak typeof(self) weakSelf = self;
[self.tableView addHeaderWithCallback:^{
        weakSelf.currentPage = 1;
        [weakSelf loadData]; //加载网络数据
    }];
    [self.tableView headerBeginRefreshing];
    
    [self.tableView addFooterWithCallback:^{
        if ((self.currentPage - 1)*KPageSize < self.totalCounts ){
        if (weakSelf.isSearching) {
     [self loadSearchMore];
}
     else {
     [weakSelf loadMore];
     }
        }else{
            
            [weakSelf.tableView footerEndRefreshing];
            [weakSelf.view makeToast:@"没有更多了"];
            
        }
        
    }];
   
     UIBarButtonItem * rightItem1  = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"3、首页-更多"] style:UIBarButtonItemStylePlain target:self action:@selector(rightaction)];
                     self.navigationItem.rightBarButtonItem = rightItem1;

    // Do any additional setup after loading the view.
}
-(void)loadData{


NSString *string = [[NSString alloc]initWithFormat:@"%@%d",_string,_currentPage];
[httpTool ZBPostNetDataWith:string withDic:nil andSuccess:^(NSDictionary* dictionary){
NSLog(@"%@",dictionary);
 if (self.currentPage ==1)
        {
            [self.dataArray removeAllObjects];
        }
_dataArray =   [NSMutableArray array];
  NSArray *array = dictionary[@"list"];
    for (NSDictionary *dic in array) {
    NSDictionary *dict = dic;
    [_dataArray addObject:dict];
    
}
  self.totalCounts =[dictionary[@"totalRow"] intValue];

[_tableView reloadData];
        [self.tableView headerEndRefreshing];
        self.currentPage++;
if (self.dataArray.count ==0) {
            [self.view makeToast:@"暂时还没数据哦"];
        }
 }andFailure:^{
         [self.tableView headerEndRefreshing];

 }];


}
-(void)loadMore{

 [MBProgressHUD showHUDAddedTo:self.view animated:YES];

NSString *string = [[NSString alloc]initWithFormat:@"%@%d",_string,_currentPage];
[httpTool ZBPostNetDataWith:string withDic:nil andSuccess:^(NSDictionary* dictionary){
 [MBProgressHUD hideHUDForView:self.view animated:YES];

NSLog(@"%@",dictionary);
 
 NSArray *array = dictionary[@"list"];
for (NSDictionary *dic in array) {
    NSDictionary *dict = dic;
    [_dataArray addObject:dict];
    
}        self.currentPage++;

  self.totalCounts =[dictionary[@"totalRow"] intValue];
        [self.tableView footerEndRefreshing];
[_tableView reloadData];
    
if (self.dataArray.count ==0) {
            [self.view makeToast:@"暂时还没数据哦"];
        }
 }andFailure:^{
        [self.tableView footerEndRefreshing];
 [MBProgressHUD hideHUDForView:self.view animated:YES];

 }];


}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (void)rightaction{
if (_scrollView!= nil) {
    [_scrollView removeFromSuperview];
            _scrollView = nil;

}else{

[self initview];
}
}
- (void)requsetdata{
[httpTool ZBPostNetDataWith:kalllist withDic:nil andSuccess:^(NSDictionary* dictionary){
 _classListarray1 = dictionary[@"classList"];
  _natureListarray1 = dictionary[@"natureList"];
 _interestListarray1 = dictionary[@"interestList"];
 _limitListarray1 = dictionary[@"limitList"];
 _repaymentListarray1 = dictionary[@"repaymentList"];
 _institutionListarray1 = dictionary[@"institutionList"];
 _guarantyListarray1 = dictionary[@"guarantyList"];
//[self initview];
 
 NSLog(@"%@",dictionary);
 }andFailure:^{
 
 }];

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

 
    return  150+10;

}
#pragma mark UITableView dataScource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

  static NSString *CellIdentifier = @"Cellcanpin";

    canpinTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
        cell=[[canpinTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
 NSDictionary *dic = self.dataArray[indexPath.row];
   if([[NSString stringWithFormat:@"%@",dic[@"flag"]]isEqualToString:@"1"]){
    cell.type =  @"tuijian";
    }
    cell.dic = dic;
    return cell;



 }

- (void)initview{
 // 创建最底层的scrollview
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0, KScreenWidth, 1)];
    _scrollView.backgroundColor = [UIColor whiteColor];
//    _scrollView.hidden = YES;
//推荐
    _tuijianListarray = [NSMutableArray array];

    [_tuijianListarray addObject:@"不限"];
    [_tuijianListarray addObject:@"推荐"];
    NSArray *tuijianListarray = [_tuijianListarray mutableCopy];
AttributeView *  tuijianattributeView = [AttributeView attributeViewWithTitle:@"产品推荐" titleFont:[UIFont boldSystemFontOfSize:14] attributeTexts:tuijianListarray viewWidth:KScreenWidth];
 _tuijianattributeView = tuijianattributeView;








    //还款方式
    _repaymentListarray = [NSMutableArray array];
    //取值时吧！=不限 &&有值
    [_repaymentListarray addObject:@"不限"];
for (NSDictionary *dic in _repaymentListarray1) {
    [_repaymentListarray addObject:dic[@"name"]];
}
    NSArray *repaymentListarray = [_repaymentListarray mutableCopy];
 AttributeView *  repaymentLisattributeView = [AttributeView attributeViewWithTitle:@"还款方式" titleFont:[UIFont boldSystemFontOfSize:14] attributeTexts:repaymentListarray viewWidth:KScreenWidth];
 _repaymentLisattributeView = repaymentLisattributeView;
//担保方式
    _guarantyListarray = [NSMutableArray array];
    //取值时吧！=不限 &&有值
    [_guarantyListarray addObject:@"不限"];
for (NSDictionary *dic in _guarantyListarray1) {
    [_guarantyListarray addObject:dic[@"name"]];
}
    NSArray *guarantyListarray = [_guarantyListarray mutableCopy];
 AttributeView *guarantyListattributeView = [AttributeView attributeViewWithTitle:@"担保方式" titleFont:[UIFont boldSystemFontOfSize:14] attributeTexts:guarantyListarray viewWidth:KScreenWidth];
 _guarantyListattributeView =guarantyListattributeView;
//产品类型
    _classListarray = [NSMutableArray array];
    //取值时吧！=不限 &&有值
    [_classListarray addObject:@"不限"];
for (NSDictionary *dic in _classListarray1) {
    [_classListarray addObject:dic[@"name"]];
}
    NSArray *classListarray = [_classListarray mutableCopy];
AttributeView *classListattributeView = [AttributeView attributeViewWithTitle:@"产品类型" titleFont:[UIFont boldSystemFontOfSize:14] attributeTexts:classListarray viewWidth:KScreenWidth];
_classListattributeView =classListattributeView;
//贷款性质
    _natureListarray = [NSMutableArray array];
    //取值时吧！=不限 &&有值
    [_natureListarray addObject:@"不限"];
for (NSDictionary *dic in _repaymentListarray1) {
    [_natureListarray addObject:dic[@"name"]];
}
    NSArray *natureListarray = [_natureListarray mutableCopy];
AttributeView *natureLisattributeView = [AttributeView attributeViewWithTitle:@"贷款性质" titleFont:[UIFont boldSystemFontOfSize:14] attributeTexts:natureListarray viewWidth:KScreenWidth];
_natureLisattributeView = natureLisattributeView;
//贷款额度
    _limitListarray = [NSMutableArray array];
    //取值时吧！=不限 &&有值
    [_limitListarray addObject:@"不限"];
for (NSDictionary *dic in _limitListarray1) {
    [_limitListarray addObject:dic[@"name"]];
}
    NSArray *limitListarray = [_limitListarray mutableCopy];
AttributeView *limitListattributeView = [AttributeView attributeViewWithTitle:@"贷款额度" titleFont:[UIFont boldSystemFontOfSize:14] attributeTexts:limitListarray viewWidth:KScreenWidth];
_limitListattributeView = limitListattributeView;
//年华利率
    _interestListarray = [NSMutableArray array];
    //取值时吧！=不限 &&有值
    [_interestListarray addObject:@"不限"];
for (NSDictionary *dic in _interestListarray1) {
    [_interestListarray addObject:dic[@"name"]];
}
    NSArray *interestListarray = [_interestListarray mutableCopy];
AttributeView *interestattributeView = [AttributeView attributeViewWithTitle:@"年华利率" titleFont:[UIFont boldSystemFontOfSize:14] attributeTexts:interestListarray viewWidth:KScreenWidth];
_interestattributeView = interestattributeView;
//所属机构
    _institutionListarray = [NSMutableArray array];
    //取值时吧！=不限 &&有值
    [_institutionListarray addObject:@"不限"];
for (NSDictionary *dic in _institutionListarray1) {
    [_institutionListarray addObject:dic[@"name"]];
}
    NSArray *institutionListarray = [_institutionListarray mutableCopy];
AttributeView *institutionListattributeView = [AttributeView attributeViewWithTitle:@"所属机构" titleFont:[UIFont boldSystemFontOfSize:14] attributeTexts:institutionListarray viewWidth:KScreenWidth];
_institutionListattributeView = institutionListattributeView;
   _tuijianattributeView.y = 0;

   _repaymentLisattributeView.y = CGRectGetMaxY(_tuijianattributeView.frame);
    _guarantyListattributeView.y = CGRectGetMaxY(_repaymentLisattributeView.frame) ;
    _classListattributeView.y = CGRectGetMaxY(_guarantyListattributeView.frame) ;
    _natureLisattributeView.y = CGRectGetMaxY(_classListattributeView.frame) ;
    _limitListattributeView.y = CGRectGetMaxY(_natureLisattributeView.frame) ;
    _interestattributeView.y = CGRectGetMaxY(_limitListattributeView.frame) ;
    _institutionListattributeView.y = CGRectGetMaxY(_interestattributeView.frame) ;
    
_tuijianattributeView.Attribute_delegate = self;

_repaymentLisattributeView.Attribute_delegate = self;
_guarantyListattributeView.Attribute_delegate = self;
_classListattributeView.Attribute_delegate = self;
_natureLisattributeView.Attribute_delegate = self;
_limitListattributeView.Attribute_delegate = self;
_interestattributeView.Attribute_delegate = self;
_institutionListattributeView.Attribute_delegate = self;

[_scrollView addSubview:_tuijianattributeView];
[_scrollView addSubview:_repaymentLisattributeView];
[_scrollView addSubview:_guarantyListattributeView];
[_scrollView addSubview:_classListattributeView];
[_scrollView addSubview:_natureLisattributeView];
[_scrollView addSubview:_limitListattributeView];
[_scrollView addSubview:_interestattributeView];
[_scrollView addSubview:_institutionListattributeView];

//    _scrollView.contentSize = (CGSize){0,[UIScreen mainScreen].bounds.size.height };
        _scrollView.contentSize = (CGSize){0,_institutionListattributeView.bottom+80 };

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake((KScreenWidth-100)/2, CGRectGetMaxY(institutionListattributeView.frame)+10, 100, 20);
        [button.layer setBorderColor:[UIColor blueColor].CGColor];
    [button.layer setBorderWidth:1];
 [button.layer setMasksToBounds:YES];
    [button setTitle:@"确定" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize: 14.0];
          [button setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
          [button setTitleColor:[UIColor whiteColor]forState:UIControlStateSelected];
        button.layer.cornerRadius = button.height/2;

    [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    [_scrollView addSubview:button];
 // 添加scrollview到当前view上
    // 通过动画设置scrollview的高度, 也可以一开始就设置好
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
    _scrollView.height = KScreenHeight/2;
//        _scrollView.height += [UIScreen mainScreen].bounds.size.height - 30;
    } completion:nil];
    [self.view addSubview:_scrollView];

}
- (void)action:(UIButton *)sender{
    _currentPage = 1;
      sender.backgroundColor = [UIColor blueColor];
_url = @"";
_url = [[NSString alloc]initWithFormat:@"%@%d",_string,_currentPage];
if(_classListString){
_url = [NSString stringWithFormat:@"%@%@",_url,_classListString];

}
if(_natureListString){
_url = [NSString stringWithFormat:@"%@%@",_url,_natureListString];

}
if(_tuijianString){
_url = [NSString stringWithFormat:@"%@%@",_url,_tuijianString];

}
if(_interestListString){
_url = [NSString stringWithFormat:@"%@%@",_url,_interestListString];

}
if(_limitListString){
_url = [NSString stringWithFormat:@"%@%@",_url,_limitListString];

}
if(_institutionListString){
_url = [NSString stringWithFormat:@"%@%@",_url,_institutionListString];

}
if(_repaymentListString){
_url = [NSString stringWithFormat:@"%@%@",_url,_repaymentListString];

}
if(_guarantyListString){
_url = [NSString stringWithFormat:@"%@%@",_url,_guarantyListString];

}
    [self.dataArray removeAllObjects];
                self.isSearching = YES;
                self.currentPage = 1;
[httpTool ZBPostNetDataWith:_url withDic:nil andSuccess:^(NSDictionary* dictionary){
NSLog(@"%@",dictionary);
 if (self.currentPage ==1)
        {
            [self.dataArray removeAllObjects];
        }
_dataArray =   [NSMutableArray array];
  NSArray *array = dictionary[@"list"];
    for (NSDictionary *dic in array) {
    NSDictionary *dict = dic;
    [_dataArray addObject:dict];
    
}
  self.totalCounts =[dictionary[@"totalRow"] intValue];

[_tableView reloadData];
   [_scrollView removeFromSuperview];
        [self.tableView headerEndRefreshing];
        self.currentPage++;
if (self.dataArray.count ==0) {
            [self.view makeToast:@"暂时还没数据哦"];
        }
 }andFailure:^{
         [self.tableView headerEndRefreshing];

 }];

}
- (void)loadSearchMore{

[httpTool ZBPostNetDataWith:_url withDic:nil andSuccess:^(NSDictionary* dictionary){
NSLog(@"%@",dictionary);
 
 NSArray *array = dictionary[@"list"];
for (NSDictionary *dic in array) {
    NSDictionary *dict = dic;
    [_dataArray addObject:dict];
    
}        self.currentPage++;

  self.totalCounts =[dictionary[@"totalRow"] intValue];
        [self.tableView footerEndRefreshing];
[_tableView reloadData];
    
if (self.dataArray.count ==0) {
            [self.view makeToast:@"暂时还没数据哦"];
        }
         if (self.tableView.headerRefreshing) {
            [self.tableView headerEndRefreshing];
        }
        if (self.tableView.footerRefreshing) {
            [self.tableView footerEndRefreshing];
        }
 }andFailure:^{
 
 if (self.tableView.headerRefreshing) {
            [self.tableView headerEndRefreshing];
        }
        if (self.tableView.footerRefreshing) {
            [self.tableView footerEndRefreshing];
        }
 }];


}
#pragma mark - AttributeViewDelegate
- (void)Attribute_View:(AttributeView *)view didClickBtn:(UIButton *)btn{
    // 判断, 根据点击不同的attributeView上的标签, 执行不同的代码
     //产品类型
    if ([view isEqual:_tuijianattributeView]&&![btn.titleLabel.text isEqualToString:@"不限"]) {

    _classListString = [NSString stringWithFormat:@"&production.flag=1"];


}
    
    
    //产品类型
    if ([view isEqual:_classListattributeView]&&![btn.titleLabel.text isEqualToString:@"不限"]) {
for (NSDictionary *dic in _classListarray1) {
    if ([dic[@"name"]isEqualToString:btn.titleLabel.text]) {
    _classListString = [NSString stringWithFormat:@"&production.productTypeid=%@",dic[@"id"]];
}
}
}
  
   //贷款性质
    if ([view isEqual:_natureLisattributeView]&&![btn.titleLabel.text isEqualToString:@"不限"]) {
for (NSDictionary *dic in _natureListarray1) {
    if ([dic[@"name"]isEqualToString:btn.titleLabel.text]) {
    _natureListString = [NSString stringWithFormat:@"&production.loanCharacterid=%@",dic[@"id"]];
}
}
}
  
  //年化利率
    if ([view isEqual:_interestattributeView]&&![btn.titleLabel.text isEqualToString:@"不限"]) {
for (NSDictionary *dic in _interestListarray1) {
    if ([dic[@"name"]isEqualToString:btn.titleLabel.text]) {
    _interestListString = [NSString stringWithFormat:@"&production.interestRate=%@",dic[@"id"]];
}
}
}
  
  
  //贷款额度
    if ([view isEqual:_limitListattributeView]&&![btn.titleLabel.text isEqualToString:@"不限"]) {
for (NSDictionary *dic in _limitListarray1) {
    if ([dic[@"name"]isEqualToString:btn.titleLabel.text]) {
    _limitListString = [NSString stringWithFormat:@"&production.maxAmount=%@",dic[@"id"]];
}
}
}

  //所属机构
    if ([view isEqual:_institutionListattributeView]&&![btn.titleLabel.text isEqualToString:@"不限"]) {
for (NSDictionary *dic in _institutionListarray1) {
    if ([dic[@"name"]isEqualToString:btn.titleLabel.text]) {
    _institutionListString = [NSString stringWithFormat:@"&production.d_cityId=%@",dic[@"id"]];
}
}
}

//还款方式
    if ([view isEqual:_repaymentLisattributeView]&&![btn.titleLabel.text isEqualToString:@"不限"]) {
for (NSDictionary *dic in _repaymentListarray1) {
    if ([dic[@"name"]isEqualToString:btn.titleLabel.text]) {
    _repaymentListString = [NSString stringWithFormat:@"&production.repaymentMethod=%@",dic[@"id"]];
}
}
}

//担保方式
    if ([view isEqual:_guarantyListattributeView]&&![btn.titleLabel.text isEqualToString:@"不限"]) {
for (NSDictionary *dic in _guarantyListarray1) {
    if ([dic[@"name"]isEqualToString:btn.titleLabel.text]) {
    _guarantyListString = [NSString stringWithFormat:@"&guaranteeMethod=%@",dic[@"id"]];
}
}
}

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

NSDictionary *dic = _dataArray[indexPath.row];
cpxqTableViewController *vc = [[cpxqTableViewController alloc]init];
vc.id = dic[@"id"];
    [self.navigationController pushViewController:vc animated:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
